package org.adit.spring.hibernate.dao;

import java.util.List;

import org.adit.spring.hibernate.entity.OrderService;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

import br.com.caelum.vraptor.ioc.Component;

@Component
public class OrderServiceDao {

	private final Session session; 
	
	public OrderServiceDao(Session session){
		this.session = session;
	}
	
	public void salva(OrderService orderService) {
		Transaction tx = session.beginTransaction();
		session.save(orderService);
		tx.commit();
	}
	
	@SuppressWarnings("unchecked")
	public List<OrderService> listaTudo(){
		return this.session.createCriteria(OrderService.class).list();
	}
	
	public OrderService carrega(Long id) {
		return (OrderService) this.session.load(OrderService.class, id);
	}
	
	public List<OrderService> carregaOrderServiceSemGeocodificacao(String nome) {
		return session.createCriteria(OrderService.class)
		.add(Restrictions.ilike("nome", nome, MatchMode.ANYWHERE))
		.list();
}
	
	
	public void atualiza(OrderService orderService) {
		Transaction tx = session.beginTransaction();
		session.update(orderService);
		tx.commit();
	}
	
	public void remove(OrderService orderService) {
		Transaction tx = session.beginTransaction();
		this.session.delete(orderService);
		tx.commit();
	}

	@SuppressWarnings("unchecked")
	public List<OrderService> busca(String nome) {
		return session.createCriteria(OrderService.class)
			.add(Restrictions.ilike("nome", nome, MatchMode.ANYWHERE))
			.list();
	}

	public void recarrega(OrderService orderService) {
		session.refresh(orderService);	
	}
}